{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "04d80e7e",
   "metadata": {},
   "source": [
    "### 实践：手写数字识别任务\n",
    "- [官网地址](https://www.paddlepaddle.org.cn/documentation/docs/zh/guides/02_paddle2.0_develop/01_quick_start_cn.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "afbce58a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import paddle\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8197cb45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2.2.1'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "paddle.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f986c17",
   "metadata": {},
   "source": [
    "#### 数据集的加载与预处理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f7c6660",
   "metadata": {},
   "source": [
    ">[paddle.vision](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/vision/Overview_cn.html)目录是 paddle 在视觉领域的最高层API\n",
    ">\n",
    ">transform.Normalize 图像归一化处理\n",
    ">\n",
    ">1、用统一的均值和标准差对图像进行归一化处理\n",
    ">\n",
    ">2、对每个通道进行不同的均值和标准差进行归一化处理\n",
    ">\n",
    "><font color=\"red\">CHW：一种数据格式，不知道真正的意思？？？</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "756c3ebf",
   "metadata": {},
   "outputs": [],
   "source": [
    "import paddle.vision.transforms as T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c6e53526",
   "metadata": {},
   "outputs": [],
   "source": [
    "transform = T.Normalize(mean=[127.5],std=[127.5],data_format=\"CHW\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "df63bff0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<paddle.vision.transforms.transforms.Normalize at 0x1b2c30eb100>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "transform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "baf07aaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下载数据集\n",
    "train_dataset = paddle.vision.MNIST(mode=\"train\",transform=transform)\n",
    "val_dataset = paddle.vision.MNIST(mode=\"test\",transform=transform)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99ad2bdd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "662db164",
   "metadata": {},
   "source": [
    "#### 设计模型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3600e62c",
   "metadata": {},
   "source": [
    ">[paddle.nn](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/Overview_cn.html) 目录下包含飞浆框架支持的神经网络层和相关的API\n",
    ">\n",
    ">Sequential 顺序容器，数据按照子层的顺序计算。**将上一层的输出结果，当作下一层的输入结果【自己的理解】**\n",
    ">\n",
    ">Flatten 展平层，将一个连续维度的 Tensor 展平为一维的 Tensor\n",
    ">\n",
    ">Linear 线性变化层，out=XW+b，其中W和b是需要学习的，784是上一层输入的单元数目，512是该层输出的单元数目\n",
    ">\n",
    ">ReLU 激活层，ReLU(x)=max(0,x) x为输入的 tensor\n",
    ">\n",
    ">Dropout 正则化层，按照给定的概率随机的将一些神经元的输出设置为0，目的是为了防止神经元节点之间过拟合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7f667b74",
   "metadata": {},
   "outputs": [],
   "source": [
    "mnist = paddle.nn.Sequential(\n",
    "    paddle.nn.Flatten(),\n",
    "    paddle.nn.Linear(784,512),\n",
    "    paddle.nn.ReLU(),\n",
    "    paddle.nn.Dropout(0.2),\n",
    "    paddle.nn.Linear(512,10)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ece0731",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "76e0db6d",
   "metadata": {},
   "source": [
    "#### 模型训练\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e8eddf6",
   "metadata": {},
   "source": [
    ">[paddle.Model](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/Model_cn.html) 一个具备模型训练、测试、推理的神经网络对象，方便模型的所有操作\n",
    ">\n",
    ">Model.prepare 配置模型所需的部件，比如：优化器、损失函数和评价指标\n",
    ">\n",
    ">paddle.optimizer.Adam 优化器，**利用梯度的一阶矩估计和二阶矩估计动态调整每个参数的学习率**\n",
    ">\n",
    ">paddle.nn.CrossEntropLoss  损失函数，计算输入input和标签label的交叉熵损失，可用于训练一个 n 类分类器\n",
    ">\n",
    ">paddle.metric.Accuracy 评估指标，计算准确率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9e5be237",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = paddle.Model(mnist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2b68c018",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.prepare(\n",
    "    paddle.optimizer.Adam(parameters=model.parameters()),\n",
    "    paddle.nn.CrossEntropyLoss(),\n",
    "    paddle.metric.Accuracy()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e47d31d5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The loss value printed in the log is the current step, and the metric is the average value of previous steps.\n",
      "Epoch 1/5\n",
      "step 938/938 [==============================] - loss: 0.2681 - acc: 0.9011 - 17ms/step          \n",
      "Epoch 2/5\n",
      "step 938/938 [==============================] - loss: 0.2362 - acc: 0.9494 - 22ms/step          \n",
      "Epoch 3/5\n",
      "step 938/938 [==============================] - loss: 0.0444 - acc: 0.9595 - 22ms/step          \n",
      "Epoch 4/5\n",
      "step 938/938 [==============================] - loss: 0.0198 - acc: 0.9638 - 23ms/step          \n",
      "Epoch 5/5\n",
      "step 938/938 [==============================] - loss: 0.1433 - acc: 0.9672 - 22ms/step          \n"
     ]
    }
   ],
   "source": [
    "model.fit(\n",
    "    train_dataset,\n",
    "    epochs=5,\n",
    "    batch_size=64,\n",
    "    verbose=1\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3f17dd4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "faa11378",
   "metadata": {},
   "source": [
    "#### 模型评估"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5196d553",
   "metadata": {},
   "source": [
    ">evaluate 在测试数据上评估模型的损失函数和评估指标\n",
    ">\n",
    ">**verbose=0，不打印日志；verbose=1，进度条打印日志；verbose=2，一行一行打印日志；**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ffa02ac6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'loss': [2.264979e-06], 'acc': 0.9724}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.evaluate(val_dataset,verbose=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "cd849672",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------------------------------------------------------------------------\n",
      " Layer (type)       Input Shape          Output Shape         Param #    \n",
      "===========================================================================\n",
      "   Flatten-2     [[64, 1, 28, 28]]        [64, 784]              0       \n",
      "   Linear-2         [[64, 784]]           [64, 512]           401,920    \n",
      "    ReLU-1          [[64, 512]]           [64, 512]              0       \n",
      "   Dropout-1        [[64, 512]]           [64, 512]              0       \n",
      "   Linear-3         [[64, 512]]            [64, 10]            5,130     \n",
      "===========================================================================\n",
      "Total params: 407,050\n",
      "Trainable params: 407,050\n",
      "Non-trainable params: 0\n",
      "---------------------------------------------------------------------------\n",
      "Input size (MB): 0.19\n",
      "Forward/backward pass size (MB): 1.14\n",
      "Params size (MB): 1.55\n",
      "Estimated Total Size (MB): 2.88\n",
      "---------------------------------------------------------------------------\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'total_params': 407050, 'trainable_params': 407050}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a07a9399",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad1692cc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
